﻿//var b = '@Url.Action("GetPatientInfo", "Radiologist")';//not run
var patientVM_Radiologist = {
    Patient: ko.observableArray([]),
    Indications: ko.observableArray([]),
    IndicationsPage: ko.observable(0),
    IndicationsPageCount: ko.observable(0),
    IndicationsPageNavHandling: function (indexController) {
        var self = this;
        if (self.IndicationsPage() > 1) {
            indexController.enableIndicationPreviousGroup(true);
        }
        if (self.IndicationsPage() < self.IndicationsPageCount()) {

            indexController.enableIndicationNextGroup(true);
        }
    },
    initialData: function() {
        var self = this;
        var patient = [{ "SocialID": "...", "Name": "...", "Birthday": null, "Male": null, "Address": "...", "Phone": "..." }];
        var indications = [{ "IndicationID": "...", "CreateDate": null, "UpdateDate": null, "IndicationInformation": "..." }];
        self.Patient(patient);
        self.Indications(indications);
    },
    sortIndicationsDESC: function () {
        this.Indications.sort(function (left, right) { return left.IndicationID == right.IndicationID ? 0 : (left.IndicationID < right.IndicationID ? 1 : -1) });
    },
    loadPatientInfo: function (searchID, searchType, indexController) {
        var self = this;
        //stop current request
        if (self.loadIndicationsPageRe != null)
            self.loadIndicationsPageRe.abort();
        //stop list indication handling
        indexController.enableListIndication(false);
        //indexController.enableBtnSearch(false);
        indexController.enableSearch(false);
        indexController.enableIndicationPreviousGroup(false);
        indexController.enableIndicationNextGroup(false);
        indexController.message.processing("processing");
        //remove old data
        self.Patient.removeAll();
        self.Indications.removeAll();
        self.IndicationsPage(0);
        self.IndicationsPageCount(0);
        $.ajax({
            type: 'post',
            url: home + 'Radiologist/GetPatientInfo',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({id: searchID, type: searchType}),
            success: function (data) {
                self.Patient(data[0]);
                self.Indications(data[1]);
                //self.sortIndicationsDESC();
                self.IndicationsPage(data[2].PageNumber);
                self.IndicationsPageCount(data[2].PageCount);
                //start list indication handling
                indexController.enableListIndication(true);
                self.IndicationsPageNavHandling(indexController);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
                //indexController.enableBtnSearch(true);
                indexController.enableSearch(true);
                indexController.searchTerm.focus();
            }
        });
    },
    loadIndicationsPageRe: null,
    loadIndicationsPage: function (indexController, pageNumber) {
        var self = this;
        //stop handling
        indexController.enableListIndication(false);
        indexController.enableIndicationPreviousGroup(false);
        indexController.enableIndicationNextGroup(false);

        indexController.message.processing("processing");
        //remove old data
        self.Indications.removeAll();
        //Ajax Call Get Patient Info
        self.loadIndicationsPageRe = $.ajax({
            type: 'post',
            url: home + 'Radiologist/GetIndicationsPage',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ PatientID: self.Patient()[0].ID, RowsPerPage: 10, PageNumber: pageNumber }),
            success: function (data) {
                self.Indications(data[0]);
                self.IndicationsPage(data[1].PageNumber);
                self.IndicationsPageCount(data[1].PageCount);
                self.IndicationsPageNavHandling(indexController);
                indexController.enableListIndication(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {//if server return string-success but no matched data
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
            }
        });
    }
};
//patientVM_Raidologist.initialData();
var IndicationsPageInfo = ko.computed(function () {
    return "Page: " + patientVM_Radiologist.IndicationsPage() + "/" + patientVM_Radiologist.IndicationsPageCount();
});